home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_emacs.idb / usr / freeware / share / emacs / 19.34 / lisp / cc-compat.el.z / cc-compat.el
Encoding:
Text File  |  1998-10-28  |  5.1 KB  |  148 lines

  1. ;;; cc-compat.el --- cc-mode compatibility with c-mode.el confusion
  2.  
  3. ;; Copyright (C) 1985, 87, 92, 93, 94, 95 Free Software Foundation, Inc.
  4.  
  5. ;; Author:        1994-1995 Barry A. Warsaw
  6. ;; Maintainer:    cc-mode-help@merlin.cnri.reston.va.us
  7. ;; Created:       August 1994, split from cc-mode.el
  8. ;; Barry Warsaw Version:       1.5
  9. ;; Keywords: c languages oop
  10.  
  11. ;; This file is part of GNU Emacs.
  12.  
  13. ;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation; either version 2, or (at your option)
  16. ;; any later version.
  17.  
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;; GNU General Public License for more details.
  22.  
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  25. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;; Boston, MA 02111-1307, USA.
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; Boring old c-mode.el (BOCM) is confusion and brain melt. cc-mode.el
  31. ;; is clarity of thought and purity of chi. If you are still unwilling
  32. ;; to accept enlightenment, this might help, or it may prolong your
  33. ;; agony.
  34. ;;
  35. ;; To use, add the following to your c-mode-hook:
  36. ;;
  37. ;; (require 'cc-compat)
  38. ;; (c-set-style "BOCM")
  39.  
  40. ;;; Code:
  41.  
  42.  
  43. ;; In case c-mode.el isn't loaded
  44. (defvar c-indent-level 2
  45.   "*Indentation of C statements with respect to containing block.")
  46. (defvar c-brace-imaginary-offset 0
  47.   "*Imagined indentation of a C open brace that actually follows a statement.")
  48. (defvar c-brace-offset 0
  49.   "*Extra indentation for braces, compared with other text in same context.")
  50. (defvar c-argdecl-indent 5
  51.   "*Indentation level of declarations of C function arguments.")
  52. (defvar c-label-offset -2
  53.   "*Offset of C label lines and case statements relative to usual indentation.")
  54. (defvar c-continued-statement-offset 2
  55.   "*Extra indent for lines not starting new statements.")
  56. (defvar c-continued-brace-offset 0
  57.   "*Extra indent for substatements that start with open-braces.
  58. This is in addition to c-continued-statement-offset.")
  59.  
  60.  
  61.  
  62. ;; these offsets are taken by brute force testing c-mode.el, since
  63. ;; there's no logic to what it does.
  64. (let* ((offsets    '(c-offsets-alist .
  65.             ((defun-block-intro     . cc-block-intro-offset)
  66.              (statement-block-intro . cc-block-intro-offset)
  67.              (defun-open            . 0)
  68.              (class-open            . 0)
  69.              (inline-open           . c-brace-offset)
  70.              (block-open            . c-brace-offset)
  71.              (block-close           . cc-block-close-offset)
  72.              (brace-list-open       . c-brace-offset)
  73.              (substatement-open     . cc-substatement-open-offset)
  74.              (substatement          . c-continued-statement-offset)
  75.              (knr-argdecl-intro     . c-argdecl-indent)
  76.              (case-label            . c-label-offset)
  77.              (access-label          . c-label-offset)
  78.              (label                 . c-label-offset)
  79.              ))))
  80.   (c-add-style "BOCM" offsets))
  81.  
  82.  
  83. (defun cc-block-intro-offset (langelem)
  84.   ;; taken directly from calculate-c-indent confusion
  85.   (save-excursion
  86.     (c-backward-syntactic-ws)
  87.     (if (= (preceding-char) ?{)
  88.     (forward-char -1)
  89.       (goto-char (cdr langelem)))
  90.     (let* ((curcol (save-excursion 
  91.              (goto-char (cdr langelem))
  92.              (current-column)))
  93.       (bocm-lossage
  94.        ;; If no previous statement, indent it relative to line
  95.        ;; brace is on.  For open brace in column zero, don't let
  96.        ;; statement start there too.  If c-indent-level is zero,
  97.        ;; use c-brace-offset + c-continued-statement-offset
  98.        ;; instead.  For open-braces not the first thing in a line,
  99.        ;; add in c-brace-imaginary-offset.
  100.        (+ (if (and (bolp) (zerop c-indent-level))
  101.           (+ c-brace-offset c-continued-statement-offset)
  102.         c-indent-level)
  103.           ;; Move back over whitespace before the openbrace.  If
  104.           ;; openbrace is not first nonwhite thing on the line,
  105.           ;; add the c-brace-imaginary-offset.
  106.           (progn (skip-chars-backward " \t")
  107.              (if (bolp) 0 c-brace-imaginary-offset))
  108.           ;; If the openbrace is preceded by a parenthesized exp,
  109.           ;; move to the beginning of that; possibly a different
  110.           ;; line
  111.           (progn
  112.         (if (eq (preceding-char) ?\))
  113.             (forward-sexp -1))
  114.         ;; Get initial indentation of the line we are on.
  115.         (current-indentation)))))
  116.       (- bocm-lossage curcol))))
  117.  
  118.  
  119. (defun cc-block-close-offset (langelem)
  120.   (save-excursion
  121.     (let* ((here (point))
  122.        bracep 
  123.        (curcol (progn
  124.              (goto-char (cdr langelem))
  125.              (current-column)))
  126.        (bocm-lossage (progn
  127.                (goto-char (cdr langelem))
  128.                (if (= (following-char) ?{)
  129.                    (setq bracep t)
  130.                  (goto-char here)
  131.                  (beginning-of-line)
  132.                  (backward-up-list 1)
  133.                  (forward-char 1)
  134.                  (c-forward-syntactic-ws))
  135.                (current-column))))
  136.       (- bocm-lossage curcol
  137.      (if bracep 0 c-indent-level)))))
  138.       
  139.  
  140. (defun cc-substatement-open-offset (langelem)
  141.   (+ c-continued-statement-offset c-continued-brace-offset))
  142.  
  143.  
  144. (provide 'cc-compat)
  145.  
  146. ;;; cc-compat.el ends here
  147.  
  148.